/*
 * @lc app=leetcode.cn id=257 lang=golang
 *
 * [257] 二叉树的所有路径
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func binaryTreePaths(root *TreeNode) []string {
	tmp, res := make([]string, 0), make([]string, 0)
	dfs(root, &tmp, &res)
	return res
}

func dfs(root *TreeNode, tmp *[]string, res *[]string) {
	if root != nil {
		*tmp = append(*tmp, strconv.Itoa(root.Val))
		if root.Left == nil && root.Right == nil {
			t := strings.Join(*tmp, "->")
			*res = append(*res, t)
		}
		if root.Left != nil {
			dfs(root.Left, tmp, res)

		}
		if root.Right != nil {
			dfs(root.Right, tmp, res)
		}
		*tmp = (*tmp)[:len(*tmp)-1]
	}
	return
}

// @lc code=end

